import javax.swing.*;
import java.awt.*;

public class Test1212 {
     JTextField jt_input;
     JTextField jt_output;

    public static void main(String[] args) {
        Test1212 t=new Test1212();
        t.addFrame(t.addjP_top(), t.addjP_bottom());//面板添加到容器
    }


    //将面板添加到容器容器
    private  void addFrame(JPanel jp_top, JPanel jb_bottom) {
        JFrame frame = new JFrame("计算器");               //创建容器，使用边框布局
        frame.setVisible(true);                               //是否可见
        frame.setSize(400, 500);                //宽，高
        frame.setLayout(new BorderLayout());                  //使用边框布局
        frame.add(jp_top, BorderLayout.NORTH);                //容器中添加面板
        frame.add(jb_bottom, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//退出窗口关闭进程
    }
//添加底部面板内容
    private  JPanel addjP_bottom() {
        JPanel  jb_bottom = new JPanel();                          // 创建下层按钮面板
        jb_bottom.setLayout(new GridLayout(4, 4));//布局方式
        //菜单布局  "0","1","2","3","4","5","6","7","8","9","+" ,"-" ,"*"," /"," =","c"
        String[] str = {"0", "1", "2", "3",
                        "4", "5", "6", "7",
                        "8", "9", "+", "-",
                        "*", "/", "=", "c"};//按钮布局
        JButton[] buttons = new JButton[str.length];
        for (int i = 0; i < str.length; i++) {
            buttons[i] = new JButton(str[i]);//按钮名称
           buttons[i].setFont(new Font("宋体", Font.PLAIN, 40));//字体大小
         buttons[i].addMouseListener(new MyMouseListener(jt_input,jt_output, buttons[i], str[i]));//放置按钮监听器
            jb_bottom.add(buttons[i]);//添加到面板
        }
        return jb_bottom;
    }
//添加顶部面板内容
    private  JPanel addjP_top() {
        JPanel   jp_top = new JPanel();                          // 创建上层按钮面板
        jp_top.setLayout(new BorderLayout());//边框布局方式
         jt_input = new JTextField();
        jp_top.add(jt_input,BorderLayout.NORTH);//顶部放置
        jt_input.setPreferredSize(new Dimension(400, 40));
        jt_input.setFont(new Font("宋体", Font.PLAIN, 35));//字体大小
        jt_output= new JTextField();
        jt_output.setFont(new Font("宋体", Font.PLAIN, 35));//字体大小
        jp_top.add(jt_output,BorderLayout.CENTER);//中间放置
        jt_output.setPreferredSize(new Dimension(400, 40));
        return jp_top;
    }
}
